<!DOCTYPE html>
<html lang=en>
<head>
  <meta charset="utf-8">
  
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="renderer" content="webkit">
  <meta http-equiv="Cache-Control" content="no-transform" />
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="format-detection" content="telephone=no,email=no,adress=no">
  <!-- Color theme for statusbar -->
  <meta name="theme-color" content="#000000" />
  <!-- 强制页面在当前窗口以独立页面显示,防止别人在框架里调用页面 -->
  <meta http-equiv="window-target" content="_top" />
  
  
  <title>数据分析学习笔记（温故篇） | 鲨鱼之家</title>
  <meta name="description" content="NumPy和Pandas基础">
<meta property="og:type" content="article">
<meta property="og:title" content="数据分析学习笔记（温故篇）">
<meta property="og:url" content="http://tina-yao.gitee.io/bigbig-shark/2022/01/05/%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E6%B8%A9%E6%95%85%E7%AF%87/index.html">
<meta property="og:site_name" content="大鲨鱼">
<meta property="og:description" content="NumPy和Pandas基础">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2022-01-05T06:38:48.000Z">
<meta property="article:modified_time" content="2022-01-18T05:12:33.933Z">
<meta property="article:author" content="BigbigShark">
<meta property="article:tag" content="Python">
<meta name="twitter:card" content="summary">
  <!-- Canonical links -->
  <link rel="canonical" href="http://tina-yao.gitee.io/bigbig-shark/2022/01/05/%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E6%B8%A9%E6%95%85%E7%AF%87/index.html">
  
    <link rel="alternate" href="/atom.xml" title="大鲨鱼" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png" type="image/x-icon">
  
  
<link rel="stylesheet" href="/bigbig-shark/css/style.css">

  
  
  
  
<meta name="generator" content="Hexo 5.4.0"></head>


<body class="main-center theme-purple# 主题颜色 theme-black theme-blue theme-green theme-purple" itemscope itemtype="http://schema.org/WebPage">
  <header class="header" itemscope itemtype="http://schema.org/WPHeader">
  <div class="slimContent">
    <div class="navbar-header">
      
      
      <div class="profile-block text-center">
        <a id="avatar" href="https://tina-yao.gitee.io/bigbig-shark/" target="_blank">
          <img class="img-circle img-rotate" src="/bigbig-shark/images/avatar.jpg" width="200" height="200">
        </a>
        <h2 id="name" class="hidden-xs hidden-sm">大鲨鱼</h2>
        <h3 id="title" class="hidden-xs hidden-sm hidden-md">CV&amp;Robots</h3>
        <small id="location" class="text-muted hidden-xs hidden-sm"><i class="icon icon-map-marker"></i> Wuhan, China</small>
      </div>
      
      <div class="search" id="search-form-wrap">

    <form class="search-form sidebar-form">
        <div class="input-group">
            <input type="text" class="search-form-input form-control" placeholder="Search" />
            <span class="input-group-btn">
                <button type="submit" class="search-form-submit btn btn-flat" onclick="return false;"><i class="icon icon-search"></i></button>
            </span>
        </div>
    </form>
    <div class="ins-search">
  <div class="ins-search-mask"></div>
  <div class="ins-search-container">
    <div class="ins-input-wrapper">
      <input type="text" class="ins-search-input" placeholder="Type something..." x-webkit-speech />
      <button type="button" class="close ins-close ins-selectable" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
    </div>
    <div class="ins-section-wrapper">
      <div class="ins-section-container"></div>
    </div>
  </div>
</div>


</div>
      <button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target="#main-navbar" aria-controls="main-navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>
    <nav id="main-navbar" class="collapse navbar-collapse" itemscope itemtype="http://schema.org/SiteNavigationElement" role="navigation">
      <ul class="nav navbar-nav main-nav menu-highlight">
        
        
        <li class="menu-item menu-item-home">
          <a href="/bigbig-shark/.">
            
            <i class="icon icon-home-fill"></i>
            
            <span class="menu-title">Home</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-archives">
          <a href="/bigbig-shark/archives">
            
            <i class="icon icon-archives-fill"></i>
            
            <span class="menu-title">Archives</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-categories">
          <a href="/bigbig-shark/categories">
            
            <i class="icon icon-folder"></i>
            
            <span class="menu-title">Categories</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-tags">
          <a href="/bigbig-shark/tags">
            
            <i class="icon icon-tags"></i>
            
            <span class="menu-title">Tags</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-links">
          <a href="/bigbig-shark/links">
            
            <i class="icon icon-friendship"></i>
            
            <span class="menu-title">Links</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-about">
          <a href="/bigbig-shark/about">
            
            <i class="icon icon-cup-fill"></i>
            
            <span class="menu-title">About</span>
          </a>
        </li>
        
      </ul>
      
	
    <ul class="social-links">
    	
        <li><a href="https://gitee.com/tina-yao" target="_blank" title="Gitee" data-toggle=tooltip data-placement=top><i class="icon icon-gitee"></i></a></li>
        
    </ul>

    </nav>
  </div>
</header>

  
    <aside class="sidebar" itemscope itemtype="http://schema.org/WPSideBar">
  <div class="slimContent">
    
      <div class="widget">
    <h3 class="widget-title">Board</h3>
    <div class="widget-body">
        <div id="board">
            <div class="content">
                <p>欢迎交流与分享经验!</p>
            </div>
        </div>
    </div>
</div>

    
      
  <div class="widget">
    <h3 class="widget-title">Categories</h3>
    <div class="widget-body">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/bigbig-shark/categories/AI/">AI</a><span class="category-list-count">1</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/bigbig-shark/categories/AI/ML/">ML</a><span class="category-list-count">1</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/bigbig-shark/categories/Concepts/">Concepts</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/bigbig-shark/categories/Robots/">Robots</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/bigbig-shark/categories/%E5%8D%8A%E6%97%A5%E9%97%B2/">半日闲</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/bigbig-shark/categories/%E6%95%B0%E5%AD%A6%E5%BB%BA%E6%A8%A1/">数学建模</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/bigbig-shark/categories/%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90/">数据分析</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/bigbig-shark/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%A7%86%E8%A7%89/">计算机视觉</a><span class="category-list-count">30</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget-body tagcloud">
      <a href="/bigbig-shark/tags/CNN-backbones/" style="font-size: 13.67px;">CNN_backbones</a> <a href="/bigbig-shark/tags/OpenCV/" style="font-size: 13px;">OpenCV</a> <a href="/bigbig-shark/tags/Python/" style="font-size: 13px;">Python</a> <a href="/bigbig-shark/tags/ROS/" style="font-size: 13px;">ROS</a> <a href="/bigbig-shark/tags/%E4%BB%A3%E7%A0%81/" style="font-size: 13px;">代码</a> <a href="/bigbig-shark/tags/%E5%85%83%E5%AE%87%E5%AE%99/" style="font-size: 13px;">元宇宙</a> <a href="/bigbig-shark/tags/%E5%85%B4%E8%B6%A3/" style="font-size: 13px;">兴趣</a> <a href="/bigbig-shark/tags/%E5%9C%BA%E6%99%AF%E6%96%87%E5%AD%97%E8%AF%86%E5%88%AB/" style="font-size: 13px;">场景文字识别</a> <a href="/bigbig-shark/tags/%E6%91%98%E6%8A%84/" style="font-size: 13.33px;">摘抄</a> <a href="/bigbig-shark/tags/%E6%95%B0%E6%8D%AE%E9%9B%86/" style="font-size: 13px;">数据集</a> <a href="/bigbig-shark/tags/%E7%9B%AE%E6%A0%87%E6%A3%80%E6%B5%8B/" style="font-size: 14px;">目标检测</a> <a href="/bigbig-shark/tags/%E7%BE%8E%E8%B5%9B/" style="font-size: 13px;">美赛</a> <a href="/bigbig-shark/tags/%E8%81%9A%E7%B1%BB/" style="font-size: 13px;">聚类</a> <a href="/bigbig-shark/tags/%E8%87%AA%E5%8A%A8%E9%A9%BE%E9%A9%B6/" style="font-size: 13.33px;">自动驾驶</a> <a href="/bigbig-shark/tags/%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%A7%86%E8%A7%893D/" style="font-size: 13px;">计算机视觉3D</a> <a href="/bigbig-shark/tags/%E8%BD%BB%E9%87%8F%E7%BA%A7/" style="font-size: 13px;">轻量级</a>
    </div>
  </div>

    
  </div>
</aside>

  
  
<main class="main" role="main">
  <div class="content">
  <article id="post-数据分析学习笔记-温故篇" class="article article-type-post" itemscope itemtype="http://schema.org/BlogPosting">
    
    <div class="article-header">
      
        
  
    <h1 class="article-title" itemprop="name">
      数据分析学习笔记（温故篇）
    </h1>
  

      
      <div class="article-meta">
        <span class="article-date">
    <i class="icon icon-calendar-check"></i>
	<a href="/bigbig-shark/2022/01/05/%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E6%B8%A9%E6%95%85%E7%AF%87/" class="article-date">
	  <time datetime="2022-01-05T06:38:48.000Z" itemprop="datePublished">2022-01-05</time>
	</a>
</span>
        
  <span class="article-category">
    <i class="icon icon-folder"></i>
    <a class="article-category-link" href="/bigbig-shark/categories/%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90/">数据分析</a>
  </span>

        
  <span class="article-tag">
    <i class="icon icon-tags"></i>
	<a class="article-tag-link-link" href="/bigbig-shark/tags/Python/" rel="tag">Python</a>
  </span>


        

	<span class="article-read hidden-xs">
    	<i class="icon icon-eye-fill" aria-hidden="true"></i>
    	<span id="/bigbig-shark/2022/01/05/%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E6%B8%A9%E6%95%85%E7%AF%87/" class="leancloud_visitors"  data-flag-title="数据分析学习笔记（温故篇）">
			<span class="leancloud-visitors-count">0</span>
		</span>
    </span>

        <span class="post-comment"><i class="icon icon-comment"></i> <a href="/bigbig-shark/2022/01/05/%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E6%B8%A9%E6%95%85%E7%AF%87/#comments" class="article-comment-link">Comments</a></span>
        
	
		<span class="post-wordcount hidden-xs" itemprop="wordCount">Word Count: 3.3k(words)</span>
	
	
		<span class="post-readcount hidden-xs" itemprop="timeRequired">Read Count: 16(minutes)</span>
	

      </div>
    </div>
    <div class="article-entry marked-body" itemprop="articleBody">
      
        <p>NumPy和Pandas基础</p>
<span id="more"></span>
<p>格式说明：在这里，博主使用&gt;&gt;&gt;表示运行结果的输出</p>
<!-- toc -->
<ul>
<li><a href="#%E5%AE%89%E8%A3%85%E5%92%8C%E5%8D%B8%E8%BD%BD%E5%8C%85">安装和卸载包</a></li>
<li><a href="#numpy">NumPy</a>
<ul>
<li><a href="#%E4%B8%8E%E5%88%97%E8%A1%A8%E7%9A%84%E5%8C%BA%E5%88%AB">与列表的区别</a></li>
<li><a href="#%E5%AF%BC%E5%85%A5">导入</a></li>
<li><a href="#%E6%95%B0%E7%BB%84%E7%9A%84%E5%88%9B%E5%BB%BA">数组的创建</a></li>
<li><a href="#%E6%95%B0%E7%BB%84%E7%9A%84%E7%B4%A2%E5%BC%95%E4%B8%8E%E5%8F%98%E6%8D%A2">数组的索引与变换</a></li>
<li><a href="#%E6%95%B0%E7%BB%84%E7%9A%84%E6%8E%92%E5%BA%8F">数组的排序</a></li>
<li><a href="#%E6%95%B0%E7%BB%84%E7%9A%84%E7%BB%84%E5%90%88">数组的组合</a></li>
<li><a href="#%E6%95%B0%E7%BB%84%E7%9A%84%E7%BB%9F%E8%AE%A1%E5%87%BD%E6%95%B0">数组的统计函数</a></li>
</ul>
</li>
<li><a href="#pandas">Pandas</a>
<ul>
<li><a href="#series">Series</a>
<ul>
<li><a href="#%E4%B8%8E%E5%AD%97%E5%85%B8%E7%9A%84%E5%8C%BA%E5%88%AB">与字典的区别</a></li>
<li><a href="#%E5%AF%BC%E5%85%A5-1">导入</a></li>
<li><a href="#series%E7%9A%84%E5%88%9B%E5%BB%BA%E4%B8%8E%E5%B1%9E%E6%80%A7">Series的创建与属性</a></li>
</ul>
</li>
<li><a href="#dataframe">DataFrame</a>
<ul>
<li><a href="#dataframe%E7%9A%84%E5%88%9B%E5%BB%BA">DataFrame的创建</a></li>
<li><a href="#dataframe%E7%9A%84%E7%B4%A2%E5%BC%95%E5%92%8C%E9%80%89%E5%8F%96">DataFrame的索引和选取</a></li>
<li><a href="#dataframe%E7%9A%84%E5%A4%9A%E9%87%8D%E7%B4%A2%E5%BC%95">DataFrame的多重索引</a></li>
<li><a href="#dataframe%E7%9A%84%E4%BF%A1%E6%81%AF">DataFrame的信息</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99">参考资料</a></li>
</ul>
<!-- tocstop -->
<h2><span id="安装和卸载包">安装和卸载包</span></h2>
<p>安装：pip install <package> 或conda install <package></package></package></p>
<p>卸载：pip uninstall <package> 或conda uninstall <package></package></package></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">pip install cvzone <span class="comment"># 安装最新版本的cvzone</span></span><br><span class="line">pip install cvzone==<span class="number">1.4</span><span class="number">.1</span> <span class="comment"># 安装指定版本的cvzone</span></span><br></pre></td></tr></table></figure>
<h2><span id="numpy">NumPy</span></h2>
<p>Numerical Python，核心功能是ndarray(n-dimensional array)，即多维数组。</p>
<h3><span id="与列表的区别">与列表的区别</span></h3>
<p>数组是同类的，即数组的所有元素必须具有相同的类型；而列表可以包含任意类型的元素。</p>
<p>使用NumPy的函数可以快速创建数组，远比使用基本库的函数节省运算时间。</p>
<h3><span id="导入">导入</span></h3>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br></pre></td></tr></table></figure>
<h3><span id="数组的创建">数组的创建</span></h3>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建一维数组</span></span><br><span class="line">np.array([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建二维数组</span></span><br><span class="line">np.array([[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>],</span><br><span class="line">          [<span class="number">2</span>, <span class="number">4</span>, <span class="number">6</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>],</span><br><span class="line">           [<span class="number">2</span>, <span class="number">4</span>, <span class="number">6</span>]])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 步长为1的等差数列，参数依次为起点(包括)、终点(不包括)、步长</span></span><br><span class="line">np.arange(<span class="number">0</span>, <span class="number">3</span>, <span class="number">1</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 总数为4个元素的等差数列，np.linspace在线性空间中以均匀步长生成数字序列</span></span><br><span class="line"><span class="comment"># 和np.arange()的功能类似，但arange()会造成精度损失，linspace不会</span></span><br><span class="line"><span class="comment"># 当已知所需的元素数量，linspace()应作为生成序列的首选(其实一般情况下两者都可以)</span></span><br><span class="line">np.linspace(<span class="number">0</span>, <span class="number">8</span>, <span class="number">4</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([<span class="number">0.</span>        , <span class="number">2.66666667</span>, <span class="number">5.33333333</span>, <span class="number">8.</span>        ])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 数组元素的连续重复复制，参数依次为待复制对象、复制次数</span></span><br><span class="line">np.repeat([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>], <span class="number">2</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([<span class="number">1</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">3</span>])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 数组元素的连续重复复制，参数依次为待复制对象、复制次数</span></span><br><span class="line">np.tile([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>], <span class="number">2</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>])</span><br><span class="line"></span><br><span class="line"><span class="comment"># A neat trick: tile-&gt;title，每一段都有头</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 生成全填充1的数组</span></span><br><span class="line">np.ones((<span class="number">2</span>, <span class="number">3</span>))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([[<span class="number">1.</span>, <span class="number">1.</span>, <span class="number">1.</span>],</span><br><span class="line">           [<span class="number">1.</span>, <span class="number">1.</span>, <span class="number">1.</span>]])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 生成全填充0的数组</span></span><br><span class="line">np.zeros((<span class="number">2</span>, <span class="number">3</span>))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([[<span class="number">0.</span>, <span class="number">0.</span>, <span class="number">0.</span>],</span><br><span class="line">           [<span class="number">0.</span>, <span class="number">0.</span>, <span class="number">0.</span>]])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 生成4个0到1之间的随机数</span></span><br><span class="line">np.random.random(<span class="number">4</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([<span class="number">0.25321095</span>, <span class="number">0.30977697</span>, <span class="number">0.18783944</span>, <span class="number">0.50184343</span>])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 生成4个服从标准正态分布的随机数</span></span><br><span class="line">np.random.randn(<span class="number">4</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([-<span class="number">1.09656944</span>, -<span class="number">0.4599131</span> , -<span class="number">1.15380812</span>,  <span class="number">0.90826068</span>])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 生成4个服从均值为1、标准差为0的正态分布</span></span><br><span class="line">np.random.normal(loc=<span class="number">0</span>, scale=<span class="number">1</span>, size=<span class="number">4</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([-<span class="number">0.07241424</span>,  <span class="number">0.66488123</span>, -<span class="number">1.05580846</span>,  <span class="number">0.33296792</span>])</span><br></pre></td></tr></table></figure>
<h3><span id="数组的索引与变换">数组的索引与变换</span></h3>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"></span><br><span class="line"><span class="comment"># 数组的构建</span></span><br><span class="line">a = np.arange(<span class="number">6</span>).reshape(<span class="number">3</span>, <span class="number">2</span>)</span><br><span class="line"><span class="comment"># 等效于a = np.reshape(np.arange(6), (3, 2))</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([[<span class="number">0</span>, <span class="number">1</span>],</span><br><span class="line">           [<span class="number">2</span>, <span class="number">3</span>],</span><br><span class="line">           [<span class="number">4</span>, <span class="number">5</span>]])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 选取某一列</span></span><br><span class="line">a[:, <span class="number">0</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([<span class="number">0</span>, <span class="number">2</span>, <span class="number">4</span>])</span><br><span class="line">a[:, <span class="number">1</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([<span class="number">1</span>, <span class="number">3</span>, <span class="number">5</span>])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 选取多列</span></span><br><span class="line">a[:, [<span class="number">0</span>, <span class="number">1</span>]]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([[<span class="number">0</span>, <span class="number">1</span>],</span><br><span class="line">           [<span class="number">2</span>, <span class="number">3</span>],</span><br><span class="line">           [<span class="number">4</span>, <span class="number">5</span>]])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 选取某一行</span></span><br><span class="line">a[<span class="number">1</span>, :]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([<span class="number">2</span>, <span class="number">3</span>])</span><br><span class="line">a[-<span class="number">1</span>, :]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([<span class="number">4</span>, <span class="number">5</span>])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 选取多行</span></span><br><span class="line">a[[<span class="number">0</span>, <span class="number">2</span>], :]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([[<span class="number">0</span>, <span class="number">1</span>],</span><br><span class="line">           [<span class="number">4</span>, <span class="number">5</span>]])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 选取某个元素，a[1, 0]即第2行第1列</span></span><br><span class="line">a[<span class="number">1</span>, <span class="number">0</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="number">2</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 数组维度的改变</span></span><br><span class="line">a.reshape(<span class="number">2</span>, <span class="number">3</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>],</span><br><span class="line">           [<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 数组的转置</span></span><br><span class="line">a.T</span><br><span class="line"><span class="comment"># 等效于np.transpose(a)</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([[<span class="number">0</span>, <span class="number">2</span>, <span class="number">4</span>],</span><br><span class="line">           [<span class="number">1</span>, <span class="number">3</span>, <span class="number">5</span>]])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 注意不要将维度的改变和转置混为一谈</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 数组的平迭展开</span></span><br><span class="line">a.flatten()</span><br><span class="line"><span class="comment"># 等效于a.ravel</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>])</span><br><span class="line"></span><br><span class="line"><span class="comment"># np.flatten()和np.ravel()都是将多维数组降为一维数组。</span></span><br><span class="line"><span class="comment"># 两者的区别在于flatten()返回一份拷贝，对拷贝所做的修改不会影响原始矩阵，</span></span><br><span class="line"><span class="comment"># 而ravel()返回的是视图，会影响原始矩阵。</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 单条件过滤</span></span><br><span class="line"><span class="comment"># 选择最后一个数字大于2的行</span></span><br><span class="line">a[a[:, <span class="number">1</span>]&gt;<span class="number">2</span>,]</span><br><span class="line"><span class="comment"># 等效于a[a[:, 1]&gt;2, :]</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([[<span class="number">2</span>, <span class="number">3</span>],</span><br><span class="line">           [<span class="number">4</span>, <span class="number">5</span>]])</span><br><span class="line"><span class="comment"># 选择最后一个数字大于4的列</span></span><br><span class="line">a[:, a[<span class="number">2</span>, :]&gt;<span class="number">4</span>]</span><br><span class="line"><span class="comment"># 等效于a[:, a[2,]&gt;4]</span></span><br><span class="line"><span class="comment"># a[, a[2, :]&gt;4]，报错，一般来说后面的冒号可以省略而前面的冒号不可以省略</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([[<span class="number">1</span>],</span><br><span class="line">           [<span class="number">3</span>],</span><br><span class="line">           [<span class="number">5</span>]])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 多条件过滤</span></span><br><span class="line">a[(a[:, <span class="number">1</span>]&gt;<span class="number">2</span>) &amp; (a[:, <span class="number">1</span>]&lt;<span class="number">4</span>),]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([[<span class="number">2</span>, <span class="number">3</span>]])</span><br></pre></td></tr></table></figure>
<h3><span id="数组的排序">数组的排序</span></h3>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"></span><br><span class="line">a = np.array([<span class="number">3</span>, <span class="number">2</span>, <span class="number">5</span>, <span class="number">4</span>])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 数组的排序，不会改变a</span></span><br><span class="line">np.sort(a)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([<span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]) </span><br><span class="line">a</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([<span class="number">3</span>, <span class="number">2</span>, <span class="number">5</span>, <span class="number">4</span>])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 数组的排序，会改变a</span></span><br><span class="line">a.sort()</span><br><span class="line">a</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span> array([<span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 多维数组的排序</span></span><br><span class="line">b = np.array([[<span class="number">1</span>, <span class="number">3</span>, <span class="number">2</span>],</span><br><span class="line">              [<span class="number">2</span>, <span class="number">1</span>, <span class="number">3</span>]])</span><br><span class="line"><span class="comment"># 按列排序，axis=0，可以理解成行与行之间进行排序</span></span><br><span class="line">b.sort(axis=<span class="number">0</span>)</span><br><span class="line">b</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([[<span class="number">1</span>, <span class="number">1</span>, <span class="number">2</span>],</span><br><span class="line">           [<span class="number">2</span>, <span class="number">3</span>, <span class="number">3</span>]])</span><br><span class="line"><span class="comment"># 按行排序，axis=1，可以理解成列与列之间进行排序</span></span><br><span class="line">b.sort(axis=<span class="number">1</span>)</span><br><span class="line">b</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>],</span><br><span class="line">           [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 数组排序后的下标，a不改变</span></span><br><span class="line">np.argsort(a)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([<span class="number">1</span>, <span class="number">0</span>, <span class="number">3</span>, <span class="number">2</span>], dtype=int64)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 数组的降序</span></span><br><span class="line">a[np.argsort(-a)]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([<span class="number">5</span>, <span class="number">4</span>, <span class="number">3</span>, <span class="number">2</span>])</span><br></pre></td></tr></table></figure>
<h3><span id="数组的组合">数组的组合</span></h3>
<p>NumPy数组的组合包括水平组合(hstack)、垂直组合(vstack)、深度组合(dstack)、列组合(colume_stack)、行组合(row_stack)。</p>
<p>对于二维数组，水平组合和列组合效果相同；垂直组合和行组合效果相同。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"></span><br><span class="line"><span class="comment"># 数组a的构造</span></span><br><span class="line">a = np.arange(<span class="number">6</span>).reshape(<span class="number">3</span>, <span class="number">2</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([[<span class="number">0</span>, <span class="number">1</span>],</span><br><span class="line">           [<span class="number">2</span>, <span class="number">3</span>],</span><br><span class="line">           [<span class="number">4</span>, <span class="number">5</span>]])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 数组b的构造</span></span><br><span class="line">b = np.arange(<span class="number">9</span>).reshape(<span class="number">3</span>, <span class="number">3</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>],</span><br><span class="line">           [<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>],</span><br><span class="line">           [<span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>]])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 数组c的构造</span></span><br><span class="line">c = np.arange(<span class="number">6</span>).reshape(<span class="number">2</span>, <span class="number">3</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>],</span><br><span class="line">           [<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 数组的水平组合</span></span><br><span class="line">np.hstack((a, b))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([[<span class="number">0</span>, <span class="number">1</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>],</span><br><span class="line">           [<span class="number">2</span>, <span class="number">3</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>],</span><br><span class="line">           [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>]])</span><br><span class="line">np.concatenate((a, b), axis=<span class="number">1</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([[<span class="number">0</span>, <span class="number">1</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>],</span><br><span class="line">           [<span class="number">2</span>, <span class="number">3</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>],</span><br><span class="line">           [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>]])</span><br><span class="line">np.append(a, b, axis=<span class="number">1</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([[<span class="number">0</span>, <span class="number">1</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>],</span><br><span class="line">           [<span class="number">2</span>, <span class="number">3</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>],</span><br><span class="line">           [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>]])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 数组的垂直组合</span></span><br><span class="line">np.vstack((b, c))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>],</span><br><span class="line">          [<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>],</span><br><span class="line">          [<span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>],</span><br><span class="line">          [<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>],</span><br><span class="line">          [<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]])</span><br><span class="line">np.concatenate((b, c), axis=<span class="number">0</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>],</span><br><span class="line">           [<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>],</span><br><span class="line">           [<span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>],</span><br><span class="line">           [<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>],</span><br><span class="line">           [<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]])</span><br><span class="line">np.append(b, c, axis=<span class="number">0</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>],</span><br><span class="line">           [<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>],</span><br><span class="line">           [<span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>],</span><br><span class="line">           [<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>],</span><br><span class="line">           [<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 前6个数来自a，后6个数来自c</span></span><br><span class="line">np.append(a, c)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>])</span><br></pre></td></tr></table></figure>
<h3><span id="数组的统计函数">数组的统计函数</span></h3>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"></span><br><span class="line">ary = np.arange(<span class="number">6</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>ary = np.arange(<span class="number">6</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 平均值、加权平均值</span></span><br><span class="line">np.mean(ary)</span><br><span class="line">np.average(ary)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 方差、标准差</span></span><br><span class="line">np.var(ary)</span><br><span class="line">np.std(ary)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 最大、最小值</span></span><br><span class="line">np.<span class="built_in">max</span>(ary)</span><br><span class="line">np.<span class="built_in">min</span>(ary)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 最大、最小值的索引值</span></span><br><span class="line">np.argmax(ary)</span><br><span class="line">np.argmin(ary)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 全距，即最大值于最小值的差</span></span><br><span class="line">np.ptp(ary)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 百分位在统计对象中的值</span></span><br><span class="line">np.percentile(ary, <span class="number">50</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="number">2.5</span></span><br><span class="line">np.percentile(ary, <span class="number">100</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="number">5.0</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 中位数</span></span><br><span class="line">np.median(ary)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 和</span></span><br><span class="line">np.<span class="built_in">sum</span>(ary)</span><br></pre></td></tr></table></figure>
<h2><span id="pandas">Pandas</span></h2>
<h3><span id="series">Series</span></h3>
<p>Series用于保存一维数据，本质上是一个带索引的一维数组、带索引的一维列表。</p>
<h4><span id="与字典的区别">与字典的区别</span></h4>
<p>Series更快，其内部是向量化运行的，和迭代相比，使用Series可以获得显著的性能上的优势。</p>
<h4><span id="导入">导入</span></h4>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br></pre></td></tr></table></figure>
<h4><span id="series的创建与属性">Series的创建与属性</span></h4>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建一个Series对象，在左侧自动生成index</span></span><br><span class="line">s = pd.Series([<span class="number">1</span>, <span class="number">3</span>, <span class="number">2</span>, <span class="number">4</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="number">0</span>    <span class="number">1</span></span><br><span class="line">    <span class="number">1</span>    <span class="number">3</span></span><br><span class="line">    <span class="number">2</span>    <span class="number">2</span></span><br><span class="line">    <span class="number">3</span>    <span class="number">4</span></span><br><span class="line">    dtype: int64</span><br><span class="line">        </span><br><span class="line"><span class="comment"># 查看values值，s.values返回一个array</span></span><br><span class="line">s.values</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([<span class="number">1</span>, <span class="number">3</span>, <span class="number">2</span>, <span class="number">4</span>], dtype=int64)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 查看index值，s.index返回一个index对象</span></span><br><span class="line">s.index</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>RangeIndex(start=<span class="number">0</span>, stop=<span class="number">4</span>, step=<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建一个Series对象，手动指定index</span></span><br><span class="line">s = pd.Series([<span class="number">1</span>, <span class="number">3</span>, <span class="number">2</span>, <span class="number">4</span>], index=[<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>, <span class="string">&#x27;d&#x27;</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a    <span class="number">1</span></span><br><span class="line">    b    <span class="number">3</span></span><br><span class="line">    c    <span class="number">2</span></span><br><span class="line">    d    <span class="number">4</span></span><br><span class="line">    dtype: int64</span><br><span class="line">        </span><br><span class="line"><span class="comment"># 查看values值</span></span><br><span class="line">s.values</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([<span class="number">1</span>, <span class="number">3</span>, <span class="number">2</span>, <span class="number">4</span>], dtype=int64)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 查看index值</span></span><br><span class="line">s.index</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>Index([<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>, <span class="string">&#x27;d&#x27;</span>], dtype=<span class="string">&#x27;object&#x27;</span>)</span><br></pre></td></tr></table></figure>
<h3><span id="dataframe">DataFrame</span></h3>
<p>DataFrame，即数据框，用于保存二维数据。和Excel表格类似。</p>
<h4><span id="dataframe的创建">DataFrame的创建</span></h4>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"></span><br><span class="line"><span class="comment"># 使用list创建</span></span><br><span class="line">df = pd.DataFrame([[<span class="string">&#x27;a&#x27;</span>, <span class="number">1</span>, <span class="number">2</span>], [<span class="string">&#x27;b&#x27;</span>, <span class="number">2</span>, <span class="number">5</span>], [<span class="string">&#x27;c&#x27;</span>, <span class="number">3</span>, <span class="number">3</span>]], columns=[<span class="string">&#x27;x&#x27;</span>, <span class="string">&#x27;y&#x27;</span>, <span class="string">&#x27;z&#x27;</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>	</span><br><span class="line">	x	y	z</span><br><span class="line"><span class="number">0</span>	a	<span class="number">1</span>	<span class="number">2</span></span><br><span class="line"><span class="number">1</span>	b	<span class="number">2</span>	<span class="number">5</span></span><br><span class="line"><span class="number">2</span>	c	<span class="number">3</span>	<span class="number">3</span></span><br><span class="line">    </span><br><span class="line"><span class="comment"># 使用ndarray创建</span></span><br><span class="line">df = pd.DataFrame(np.zeros((<span class="number">3</span>, <span class="number">3</span>)), columns=[<span class="string">&#x27;x&#x27;</span>, <span class="string">&#x27;y&#x27;</span>, <span class="string">&#x27;z&#x27;</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span></span><br><span class="line">	x	y	z</span><br><span class="line"><span class="number">0</span>	<span class="number">0.0</span>	<span class="number">0.0</span>	<span class="number">0.0</span></span><br><span class="line"><span class="number">1</span>	<span class="number">0.0</span>	<span class="number">0.0</span>	<span class="number">0.0</span></span><br><span class="line"><span class="number">2</span>	<span class="number">0.0</span>	<span class="number">0.0</span>	<span class="number">0.0</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 使用字典创建，字典的键变为DataFrame的列索引。</span></span><br><span class="line">df = pd.DataFrame(&#123;<span class="string">&#x27;x&#x27;</span>: [<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>], <span class="string">&#x27;y&#x27;</span>: <span class="built_in">range</span>(<span class="number">1</span>, <span class="number">4</span>), <span class="string">&#x27;z&#x27;</span>: [<span class="number">2</span>, <span class="number">5</span>, <span class="number">3</span>]&#125;)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span></span><br><span class="line">	x	y	z</span><br><span class="line"><span class="number">0</span>	a	<span class="number">1</span>	<span class="number">2</span></span><br><span class="line"><span class="number">1</span>	b	<span class="number">2</span>	<span class="number">5</span></span><br><span class="line"><span class="number">2</span>	c	<span class="number">3</span>	<span class="number">3</span></span><br><span class="line"></span><br><span class="line">df = pd.DataFrame(<span class="built_in">dict</span>(x=[<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>], y=<span class="built_in">range</span>(<span class="number">1</span>, <span class="number">4</span>), z=[<span class="number">2</span>, <span class="number">5</span>, <span class="number">3</span>]))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>	</span><br><span class="line">	x	y	z</span><br><span class="line"><span class="number">0</span>	a	<span class="number">1</span>	<span class="number">2</span></span><br><span class="line"><span class="number">1</span>	b	<span class="number">2</span>	<span class="number">5</span></span><br><span class="line"><span class="number">2</span>	c	<span class="number">3</span>	<span class="number">3</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建空数据框</span></span><br><span class="line">df = pd.DataFrame(columns=[<span class="string">&#x27;x&#x27;</span>, <span class="string">&#x27;y&#x27;</span>, <span class="string">&#x27;z&#x27;</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>	</span><br><span class="line">	x	y	z</span><br><span class="line">        _________________</span><br><span class="line"></span><br><span class="line"><span class="comment"># 网格分布型数据的创建</span></span><br><span class="line">a = [<span class="string">&#x27;A&#x27;</span>, <span class="string">&#x27;B&#x27;</span>, <span class="string">&#x27;C&#x27;</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>[<span class="string">&#x27;A&#x27;</span>, <span class="string">&#x27;B&#x27;</span>, <span class="string">&#x27;C&#x27;</span>]</span><br><span class="line">b = [<span class="number">5</span>, <span class="number">7</span>, <span class="number">9</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>[<span class="number">5</span>, <span class="number">7</span>, <span class="number">9</span>]</span><br><span class="line">X, Y = np.meshgrid(a, b)</span><br><span class="line">X</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span> array([[<span class="string">&#x27;A&#x27;</span>, <span class="string">&#x27;B&#x27;</span>, <span class="string">&#x27;C&#x27;</span>],</span><br><span class="line">            [<span class="string">&#x27;A&#x27;</span>, <span class="string">&#x27;B&#x27;</span>, <span class="string">&#x27;C&#x27;</span>],</span><br><span class="line">            [<span class="string">&#x27;A&#x27;</span>, <span class="string">&#x27;B&#x27;</span>, <span class="string">&#x27;C&#x27;</span>]], dtype=<span class="string">&#x27;&lt;U1&#x27;</span>)</span><br><span class="line">Y</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>array([[<span class="number">5</span>, <span class="number">5</span>, <span class="number">5</span>],</span><br><span class="line">           [<span class="number">7</span>, <span class="number">7</span>, <span class="number">7</span>],</span><br><span class="line">           [<span class="number">9</span>, <span class="number">9</span>, <span class="number">9</span>]])</span><br><span class="line"></span><br><span class="line">df = pd.DataFrame(&#123;<span class="string">&#x27;x&#x27;</span>: X.flatten(), <span class="string">&#x27;y&#x27;</span>: Y.flatten()&#125;)</span><br><span class="line">&gt;&gt;&gt;</span><br><span class="line">	x	y</span><br><span class="line"><span class="number">0</span>	A	<span class="number">5</span></span><br><span class="line"><span class="number">1</span>	B	<span class="number">5</span></span><br><span class="line"><span class="number">2</span>	C	<span class="number">5</span></span><br><span class="line"><span class="number">3</span>	A	<span class="number">7</span></span><br><span class="line"><span class="number">4</span>	B	<span class="number">7</span></span><br><span class="line"><span class="number">5</span>	C	<span class="number">7</span></span><br><span class="line"><span class="number">6</span>	A	<span class="number">9</span></span><br><span class="line"><span class="number">7</span>	B	<span class="number">9</span></span><br><span class="line"><span class="number">8</span>	C	<span class="number">9</span></span><br></pre></td></tr></table></figure>
<h4><span id="dataframe的索引和选取">DataFrame的索引和选取</span></h4>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"></span><br><span class="line"><span class="comment"># 构建一个DataFrame对象</span></span><br><span class="line">df = pd.DataFrame(&#123;<span class="string">&#x27;x&#x27;</span>: [<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>], <span class="string">&#x27;y&#x27;</span>: <span class="built_in">range</span>(<span class="number">1</span>, <span class="number">4</span>), <span class="string">&#x27;z&#x27;</span>: [<span class="number">2</span>, <span class="number">5</span>, <span class="number">3</span>]&#125;)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>	</span><br><span class="line">	x	y	z</span><br><span class="line"><span class="number">0</span>	a	<span class="number">1</span>	<span class="number">2</span></span><br><span class="line"><span class="number">1</span>	b	<span class="number">2</span>	<span class="number">5</span></span><br><span class="line"><span class="number">2</span>	c	<span class="number">3</span>	<span class="number">3</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 选取某一列</span></span><br><span class="line">df[<span class="string">&#x27;y&#x27;</span>]</span><br><span class="line"><span class="comment"># 等效于df.y</span></span><br><span class="line"><span class="comment"># 等效于df.iloc[:, 1]</span></span><br><span class="line"><span class="comment"># 等效于df.loc[:, &#x27;y&#x27;]</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="number">0</span>    <span class="number">1</span></span><br><span class="line">    <span class="number">1</span>    <span class="number">2</span></span><br><span class="line">    <span class="number">2</span>    <span class="number">3</span></span><br><span class="line">    Name: y, dtype: int64</span><br><span class="line"></span><br><span class="line"><span class="comment"># 选取某一列</span></span><br><span class="line">df.loc[:, [<span class="string">&#x27;y&#x27;</span>]]</span><br><span class="line"><span class="comment"># 等效于df.iloc[:, [1]]</span></span><br><span class="line"><span class="comment"># A neat trick: iloc理解成integer</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span></span><br><span class="line">	y</span><br><span class="line"><span class="number">0</span>	<span class="number">1</span></span><br><span class="line"><span class="number">1</span>	<span class="number">2</span></span><br><span class="line"><span class="number">2</span>	<span class="number">3</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 选取多列</span></span><br><span class="line">df[[<span class="string">&#x27;x&#x27;</span>, <span class="string">&#x27;y&#x27;</span>]]</span><br><span class="line"><span class="comment"># 等效于df.loc[:, [&#x27;x&#x27;, &#x27;y&#x27;]]</span></span><br><span class="line"><span class="comment"># 等效于df.iloc[:, [0, 1]]</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>	</span><br><span class="line">	x	y</span><br><span class="line"><span class="number">0</span>	a	<span class="number">1</span></span><br><span class="line"><span class="number">1</span>	b	<span class="number">2</span></span><br><span class="line"><span class="number">2</span>	c	<span class="number">3</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 选取某一行</span></span><br><span class="line">df.loc[<span class="number">1</span>, :]</span><br><span class="line"><span class="comment"># 等效于df.loc[1,]</span></span><br><span class="line"><span class="comment"># 等效于df.iloc[1, :]</span></span><br><span class="line"><span class="comment"># 等效于df.iloc[1,]</span></span><br><span class="line"><span class="comment"># 为了养成良好的编程习惯，我选择一直使用冒号&#x27;:&#x27;，后面不再区分</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x    b</span><br><span class="line">    y    <span class="number">2</span></span><br><span class="line">    z    <span class="number">5</span></span><br><span class="line">    Name: <span class="number">1</span>, dtype: <span class="built_in">object</span></span><br><span class="line">        </span><br><span class="line"><span class="comment"># 选取多行</span></span><br><span class="line">df.loc[[<span class="number">0</span>, <span class="number">2</span>], :]</span><br><span class="line"><span class="comment"># df.iloc[[0, 2], :]</span></span><br><span class="line">&gt;&gt;&gt;</span><br><span class="line">	x	y	z</span><br><span class="line"><span class="number">0</span>	a	<span class="number">1</span>	<span class="number">2</span></span><br><span class="line"><span class="number">2</span>	c	<span class="number">3</span>	<span class="number">3</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 选取某个元素</span></span><br><span class="line">df.loc[<span class="number">1</span>, <span class="string">&#x27;y&#x27;</span>]</span><br><span class="line"><span class="comment"># 等效于df.iloc[1, 1]</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="number">2</span></span><br><span class="line"></span><br><span class="line">df.loc[[<span class="number">1</span>], <span class="string">&#x27;y&#x27;</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="number">1</span>    <span class="number">2</span></span><br><span class="line">    Name: y, dtype: int64</span><br><span class="line">            </span><br><span class="line"><span class="comment"># 单条件过滤</span></span><br><span class="line">df[df.z&gt;=<span class="number">3</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span></span><br><span class="line">	x	y	z</span><br><span class="line"><span class="number">1</span>	b	<span class="number">2</span>	<span class="number">5</span></span><br><span class="line"><span class="number">2</span>	c	<span class="number">3</span>	<span class="number">3</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 多条件过滤</span></span><br><span class="line">df[(df.z&gt;=<span class="number">3</span>) &amp; (df.z&lt;=<span class="number">4</span>)]</span><br><span class="line"><span class="comment"># 等效于df.query(&#x27;z&gt;=3 &amp; z&lt;=4&#x27;)</span></span><br><span class="line">&gt;&gt;&gt;</span><br><span class="line">	x	y	z</span><br><span class="line"><span class="number">2</span>	c	<span class="number">3</span>	<span class="number">3</span></span><br></pre></td></tr></table></figure>
<h4><span id="dataframe的多重索引">DataFrame的多重索引</span></h4>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"></span><br><span class="line"><span class="comment"># 构建原始DataFrame对象</span></span><br><span class="line">df = pd.DataFrame(<span class="built_in">dict</span>(X=[<span class="string">&#x27;A&#x27;</span>, <span class="string">&#x27;B&#x27;</span>, <span class="string">&#x27;C&#x27;</span>, <span class="string">&#x27;A&#x27;</span>, <span class="string">&#x27;B&#x27;</span>, <span class="string">&#x27;C&#x27;</span>], year=[<span class="number">2010</span>, <span class="number">2010</span>, <span class="number">2010</span>, <span class="number">2011</span>, <span class="number">2011</span>, <span class="number">2011</span>], value=[<span class="number">1</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">2</span>]))</span><br><span class="line">&gt;&gt;&gt;</span><br><span class="line">	X	year	value</span><br><span class="line"><span class="number">0</span>	A	<span class="number">2010</span>	<span class="number">1</span></span><br><span class="line"><span class="number">1</span>	B	<span class="number">2010</span>	<span class="number">3</span></span><br><span class="line"><span class="number">2</span>	C	<span class="number">2010</span>	<span class="number">4</span></span><br><span class="line"><span class="number">3</span>	A	<span class="number">2011</span>	<span class="number">3</span></span><br><span class="line"><span class="number">4</span>	B	<span class="number">2011</span>	<span class="number">5</span></span><br><span class="line"><span class="number">5</span>	C	<span class="number">2011</span>	<span class="number">2</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 设定df的索引为X，year。df本身不会改变</span></span><br><span class="line">df.set_index([<span class="string">&#x27;X&#x27;</span>, <span class="string">&#x27;year&#x27;</span>])</span><br><span class="line">&gt;&gt;&gt;</span><br><span class="line">		value</span><br><span class="line">X	year	</span><br><span class="line">A	<span class="number">2010</span>	<span class="number">1</span></span><br><span class="line">B	<span class="number">2010</span>	<span class="number">3</span></span><br><span class="line">C	<span class="number">2010</span>	<span class="number">4</span></span><br><span class="line">A	<span class="number">2011</span>	<span class="number">3</span></span><br><span class="line">B	<span class="number">2011</span>	<span class="number">5</span></span><br><span class="line">C	<span class="number">2011</span>	<span class="number">2</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 存疑</span></span><br><span class="line"><span class="comment"># 选取某个元素</span></span><br><span class="line">df.loc[(<span class="string">&#x27;A&#x27;</span>, <span class="number">2010</span>), <span class="string">&#x27;value&#x27;</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="number">1</span></span><br><span class="line">df.loc[(<span class="string">&#x27;A&#x27;</span>, <span class="built_in">slice</span>(<span class="literal">None</span>)), <span class="string">&#x27;value&#x27;</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="number">1</span> <span class="number">3</span></span><br></pre></td></tr></table></figure>
<h4><span id="dataframe的信息">DataFrame的信息</span></h4>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"></span><br><span class="line">df = pd.DataFrame(&#123;<span class="string">&#x27;x&#x27;</span>: [<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>], <span class="string">&#x27;y&#x27;</span>: <span class="built_in">range</span>(<span class="number">1</span>, <span class="number">4</span>), <span class="string">&#x27;z&#x27;</span>: [<span class="number">2</span>, <span class="number">5</span>, <span class="number">3</span>]&#125;)</span><br><span class="line">&gt;&gt;&gt;</span><br><span class="line">	x	y	z</span><br><span class="line"><span class="number">0</span>	a	<span class="number">1</span>	<span class="number">2</span></span><br><span class="line"><span class="number">1</span>	b	<span class="number">2</span>	<span class="number">5</span></span><br><span class="line"><span class="number">2</span>	c	<span class="number">3</span>	<span class="number">3</span></span><br><span class="line">    </span><br><span class="line"><span class="comment"># 获取前几行，默认是5</span></span><br><span class="line">df.head()</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span></span><br><span class="line">	x	y	z</span><br><span class="line"><span class="number">0</span>	a	<span class="number">1</span>	<span class="number">2</span></span><br><span class="line"><span class="number">1</span>	b	<span class="number">2</span>	<span class="number">5</span></span><br><span class="line"><span class="number">2</span>	c	<span class="number">3</span>	<span class="number">3</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#获取后几行，默认是5</span></span><br><span class="line">df.tail()</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取属性信息</span></span><br><span class="line">df.info()</span><br><span class="line">&gt;&gt;&gt;</span><br><span class="line">&lt;<span class="class"><span class="keyword">class</span> &#x27;<span class="title">pandas</span>.<span class="title">core</span>.<span class="title">frame</span>.<span class="title">DataFrame</span>&#x27;&gt;</span></span><br><span class="line"><span class="class"><span class="title">RangeIndex</span>:</span> <span class="number">3</span> entries, <span class="number">0</span> to <span class="number">2</span></span><br><span class="line">Data columns (total <span class="number">3</span> columns):</span><br><span class="line"> <span class="comment">#   Column  Non-Null Count  Dtype </span></span><br><span class="line">---  ------  --------------  ----- </span><br><span class="line"> <span class="number">0</span>   x       <span class="number">3</span> non-null      <span class="built_in">object</span></span><br><span class="line"> <span class="number">1</span>   y       <span class="number">3</span> non-null      int64 </span><br><span class="line"> <span class="number">2</span>   z       <span class="number">3</span> non-null      int64 </span><br><span class="line">dtypes: int64(<span class="number">2</span>), <span class="built_in">object</span>(<span class="number">1</span>)</span><br><span class="line">memory usage: <span class="number">200.0</span>+ <span class="built_in">bytes</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取行数</span></span><br><span class="line">df.shape[<span class="number">0</span>]</span><br><span class="line"><span class="built_in">len</span>(df)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取列数</span></span><br><span class="line">df.shape[<span class="number">1</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取维数</span></span><br><span class="line">df.shape</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取数据个数</span></span><br><span class="line">df.size</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取列名</span></span><br><span class="line">df.columns</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>Index([<span class="string">&#x27;x&#x27;</span>, <span class="string">&#x27;y&#x27;</span>, <span class="string">&#x27;z&#x27;</span>], dtype=<span class="string">&#x27;object&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取行名</span></span><br><span class="line">df.index</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>RangeIndex(start=<span class="number">0</span>, stop=<span class="number">3</span>, step=<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 存疑</span></span><br><span class="line"><span class="comment"># 重新定义列名，必须使用双引号，单引号会报错</span></span><br><span class="line">df.colunms = [<span class="string">&quot;X&quot;</span>, <span class="string">&quot;Y&quot;</span>, <span class="string">&quot;Z&quot;</span>]</span><br><span class="line"><span class="comment"># 但是这样并没有成功修改df的列名，有什么用</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 更改某列的列名</span></span><br><span class="line">df.rename(columns=&#123;<span class="string">&#x27;x&#x27;</span>: <span class="string">&#x27;X&#x27;</span>&#125;, inplace=<span class="literal">True</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span></span><br><span class="line">	X	y	z</span><br><span class="line"><span class="number">0</span>	a	<span class="number">1</span>	<span class="number">2</span></span><br><span class="line"><span class="number">1</span>	b	<span class="number">2</span>	<span class="number">5</span></span><br><span class="line"><span class="number">2</span>	c	<span class="number">3</span>	<span class="number">3</span></span><br><span class="line"><span class="comment"># 如果缺少inplace，则不会更改，而是增加新列</span></span><br><span class="line">df.rename(columns=&#123;<span class="string">&#x27;y&#x27;</span>: <span class="string">&#x27;Y&#x27;</span>&#125;)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span></span><br><span class="line">	X	Y	z</span><br><span class="line"><span class="number">0</span>	a	<span class="number">1</span>	<span class="number">2</span></span><br><span class="line"><span class="number">1</span>	b	<span class="number">2</span>	<span class="number">5</span></span><br><span class="line"><span class="number">2</span>	c	<span class="number">3</span>	<span class="number">3</span></span><br><span class="line"><span class="comment"># 但df没有改变</span></span><br><span class="line">df</span><br><span class="line">&gt;&gt;&gt;</span><br><span class="line">	X	y	z</span><br><span class="line"><span class="number">0</span>	a	<span class="number">1</span>	<span class="number">2</span></span><br><span class="line"><span class="number">1</span>	b	<span class="number">2</span>	<span class="number">5</span></span><br><span class="line"><span class="number">2</span>	c	<span class="number">3</span>	<span class="number">3</span></span><br></pre></td></tr></table></figure>
<h2><span id="参考资料">参考资料</span></h2>
<p>《Python数据可视化之美》_刘杰</p>

      
    </div>
    <div class="article-footer">
      <blockquote class="mt-2x">
  <ul class="post-copyright list-unstyled">
    
    <li class="post-copyright-link hidden-xs">
      <strong>本文链接：</strong>
      <a href="http://tina-yao.gitee.io/bigbig-shark/2022/01/05/%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E6%B8%A9%E6%95%85%E7%AF%87/" title="数据分析学习笔记（温故篇）" target="_blank" rel="external">http://tina-yao.gitee.io/bigbig-shark/2022/01/05/数据分析学习笔记-温故篇/</a>
    </li>
    
    <li class="post-copyright-license">
      <strong>版权声明： </strong> 本博客所有文章除特别声明外，均采用 <a href="http://creativecommons.org/licenses/by/4.0/deed.zh" target="_blank" rel="external">CC BY 4.0 CN协议</a> 许可协议。转载请注明出处！
    </li>
  </ul>
</blockquote>


<div class="panel panel-default panel-badger">
  <div class="panel-body">
    <figure class="media">
      <div class="media-left">
        <a href="https://tina-yao.gitee.io/bigbig-shark/" target="_blank" class="img-burn thumb-sm visible-lg">
          <img src="/bigbig-shark/images/avatar.jpg" class="img-rounded w-full" alt="">
        </a>
      </div>
      <div class="media-body">
        <h3 class="media-heading"><a href="https://tina-yao.gitee.io/bigbig-shark/" target="_blank"><span class="text-dark">大鲨鱼</span><small class="ml-1x">CV&amp;Robots</small></a></h3>
        <div>格物致知，诚意力行。</div>
      </div>
    </figure>
  </div>
</div>


    </div>
  </article>
  
    
  <section id="comments">
  	
      <div id="vcomments"></div>
    
  </section>


  
</div>

  <nav class="bar bar-footer clearfix" data-stick-bottom>
  <div class="bar-inner">
  
  <ul class="pager pull-left">
    
    <li class="prev">
      <a href="/bigbig-shark/2022/02/28/2022%E7%BE%8E%E8%B5%9B%E7%BB%8F%E5%8E%86%E5%8F%8A%E4%BD%93%E4%BC%9A/" title="2022美赛总结"><i class="icon icon-angle-left" aria-hidden="true"></i><span>&nbsp;&nbsp;Newer</span></a>
    </li>
    
    
    <li class="next">
      <a href="/bigbig-shark/2021/12/25/%E5%88%9D%E8%AF%86%E5%9C%BA%E6%99%AF%E6%96%87%E5%AD%97%E8%AF%86%E5%88%AB/" title="初识场景文字识别"><span>Older&nbsp;&nbsp;</span><i class="icon icon-angle-right" aria-hidden="true"></i></a>
    </li>
    
    
  </ul>
  
  
  <!-- Button trigger modal -->
  <button type="button" class="btn btn-fancy btn-donate pop-onhover bg-gradient-warning" data-toggle="modal" data-target="#donateModal"><span>$</span></button>
  <!-- <div class="wave-icon wave-icon-danger btn-donate" data-toggle="modal" data-target="#donateModal">
    <div class="wave-circle"><span class="icon"><i class="icon icon-bill"></i></span></div>
  </div> -->
  
  
  <div class="bar-right">
    
    <div class="share-component" data-sites="weibo,qq,wechat" data-mobile-sites="weibo,qq"></div>
    
  </div>
  </div>
</nav>
  
<!-- Modal -->
<div class="modal modal-center modal-small modal-xs-full fade" id="donateModal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content donate">
      <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      <div class="modal-body">
        <div class="donate-box">
          <div class="donate-head">
            <p>Maybe you could buy me a cup of coffee.</p>
          </div>
          <div class="tab-content">
            <div role="tabpanel" class="tab-pane fade active in" id="alipay">
              <div class="donate-payimg">
                <img src="https://gitee.com/tina-yao/bigbig-shark/raw/master/images/donate/alipayimg.PNG#images/donate/alipayimg.png" alt="Scan Qrcode" title="Scan" />
              </div>
              <p class="text-muted mv">Scan this qrcode</p>
              <p class="text-grey">Open alipay app scan this qrcode, buy me a coffee!</p>
            </div>
            <div role="tabpanel" class="tab-pane fade" id="wechatpay">
              <div class="donate-payimg">
                <img src="https://gitee.com/tina-yao/bigbig-shark/raw/master/images/donate/wechatpayimg.PNG#images/donate/wechatpayimg.png" alt="Scan Qrcode" title="Scan" />
              </div>
              <p class="text-muted mv">Scan this qrcode</p>
              <p class="text-grey">Open wechat app scan this qrcode, buy me a coffee!</p>
            </div>
          </div>
          <div class="donate-footer">
            <ul class="nav nav-tabs nav-justified" role="tablist">
              <li role="presentation" class="active">
                <a href="#alipay" id="alipay-tab" role="tab" data-toggle="tab" aria-controls="alipay" aria-expanded="true"><i class="icon icon-alipay"></i> alipay</a>
              </li>
              <li role="presentation" class="">
                <a href="#wechatpay" role="tab" id="wechatpay-tab" data-toggle="tab" aria-controls="wechatpay" aria-expanded="false"><i class="icon icon-wepay"></i> wechat payment</a>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>



</main>

  <footer class="footer" itemscope itemtype="http://schema.org/WPFooter">
	
	
    <ul class="social-links">
    	
        <li><a href="https://gitee.com/tina-yao" target="_blank" title="Gitee" data-toggle=tooltip data-placement=top><i class="icon icon-gitee"></i></a></li>
        
    </ul>

    <div class="copyright">
    	
        &copy; 2023 BigbigShark
        
        <div class="publishby">
        <!--
        	Theme by <a href="https://github.com/cofess" target="_blank"> cofess </a>base on <a href="https://github.com/cofess/hexo-theme-pure" target="_blank">pure</a>.
        -->
        </div>
    </div>
</footer>
  <script src="//cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<script>
window.jQuery || document.write('<script src="js/jquery.min.js"><\/script>')
</script>

<script src="/bigbig-shark/js/plugin.min.js"></script>


<script src="/bigbig-shark/js/application.js"></script>


    <script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: 'Posts',
            PAGES: 'Pages',
            CATEGORIES: 'Categories',
            TAGS: 'Tags',
            UNTITLED: '(Untitled)',
        },
        ROOT_URL: '/bigbig-shark/',
        CONTENT_URL: '/bigbig-shark/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>

<script src="/bigbig-shark/js/insight.js"></script>






   




   
    
  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/valine"></script>
  <script type="text/javascript">
  var GUEST = ['nick', 'mail', 'link'];
  var meta = 'nick,mail,link';
  meta = meta.split(',').filter(function(item) {
    return GUEST.indexOf(item) > -1;
  });
  new Valine({
    el: '#vcomments',
    verify: false,
    notify: false,
    appId: '8pTCCvyyLGXskH0XW0fWHg7l-gzGzoHsz',
    appKey: 'id3rlCPRdbEoSpUfhJgcLhDO',
    placeholder: 'Just go go',
    avatar: 'mm',
    meta: meta,
    pageSize: '10' || 10,
    visitor: true
  });
  </script>

     







<script src="/bigbig-shark/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginModelPath":"assets/","model":{"jsonPath":"/bigbig-shark/live2dw/assets/tororo.model.json"},"display":{"position":null,"width":300,"height":600},"log":false,"pluginJsPath":"lib/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>